﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ADLite;
using System.Threading;

namespace AmiBrokerPlugin
{
    /// <summary>
    /// All public static methods of this class will be exported for use by AmiBroker
    /// </summary>
    public static class Plugin
    {
        private static AlfaDirectService ad = new AlfaDirectService();

        #region Common Exported Functions

        /////////////////////////////////////////////////////////////////////////
        // COMMON EXPORTED FUNCTONS
        //
        // Each AmiBroker plug-in DLL must export the following
        // functions:
        // 1. GetPluginInfo	- called when DLL is loaded
        // 2. Init - called when AFL engine is being initialized 
        // 3. Release - called when AFL engine is being closed
        /////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Called when DLL is loaded
        /// </summary>
        public static int GetPluginInfo(ref PluginInfo pluginInfo)
        {
            pluginInfo.Name = "Alfa-Direct® data Plug-in";
            pluginInfo.Vendor = "AmiBroker.CodePlex.com";
            pluginInfo.Type = PluginType.Data;
            pluginInfo.Version = 10000;
            pluginInfo.IDCode = GetIDCode("ALFA");
            pluginInfo.Certificate = 0;
            pluginInfo.MinAmiVersion = 387000;
            pluginInfo.StructSize = Marshal.SizeOf(pluginInfo);

            return 1;
        }

        /// <summary>
        /// Called when AFL engine is being initialized
        /// </summary>
        public static int Init()
        {
            // TODO: Add initialization logic here
            return 1;
        }

        /// <summary>
        /// Called when AFL engine is being closed
        /// </summary>
        public static int Release()
        {
            // TODO: Add cleanup logic here
            return 1;
        }

        #endregion

        #region Data Exported Functions

        unsafe public static int GetQuotesEx(string ticker, int periodicity, int lastValid, int size, Quotation* quotes, GQEContext* context)
        {
            var emitents = ad.Emitents;
            var exchanges = ad.Exchanges;
            var tickers = ad.Tickers;
            //var tmp = ad.GetFields("papers");

            return lastValid + 1;
        }

        #endregion

        #region Private Helper Funcions

        /// <summary>
        /// Converts string code into Int32 required by AmiBroker
        /// </summary>
        private static int GetIDCode(string id)
        {
            if (id.Length != 4)
            {
                throw new ArgumentException("Plugin ID must be 4 characters long.", "id");
            }

            return id[0] << 24 | id[1] << 16 | id[2] << 8 | id[3] << 0;
        }

        private static ulong GetDate(DateTime date)
        {
            return (ulong)date.Year << 52 | (ulong)date.Month << 48 | (ulong)date.Day << 43 | (ulong)date.Hour << 38;
        }

        #endregion
    }
}
